package com.xsherl.leetcode.solution;

public class FindMaximumXOR {

    public int findMaximumXOR(int[] nums) {
        int maxLen = 0, maxXOR = 0;
        for (int i = 0; i < nums.length; ++i) {
            int m = nums[i];
            int len = getLen(m);
            if (len < maxLen) {
                continue;
            }
            maxLen = len;
            for (int j = 0; j < nums.length; ++j) {
                int t = nums[j] ^ m;
                if (t > maxXOR) {
                    maxXOR = t;
                }
            }
        }

        return maxXOR;
    }

    private int getLen(int t) {
        int len = 0;
        while (t != 0) {
            t >>= 1;
            len++;
        }
        return len;
    }

}
